package cn.zclong.user.web.servlet.admin;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;
import cn.zclong.user.domain.User;
import cn.zclong.user.service.UserException;
import cn.zclong.user.service.UserService;


public class AdminUserServlet extends BaseServlet {
	private UserService userService = new UserService();
	
	
	/**
	 * 管理员修改用户
	 */
	public String edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 1，封装表单数据
		 * 2，调用service方法完成修改工作
		 * 3，调用findAll()
		 */
		
		User user = CommonUtils.toBean(request.getParameterMap(), User.class);
		try {
			userService.edit(user);
			return findAll(request, response);
		} catch (UserException e) {
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("user", user);
			return "f:/adminjsps/admin/user/mod.jsp";
		}
		
	}
	
	
	/**
	 * 管理员修改用户之前的加载工作
	 */
	public String editPre(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uid = request.getParameter("uid");
		request.setAttribute("user", userService.load(uid));
		return "f:/adminjsps/admin/user/mod.jsp";
	}
	/**
	 * 管理员添加用户
	 */
	public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 1，获取表单数据
		 * 2，给uid赋值
		 * 3，调用service方法
		 * 4，调用findAll()方法
		 */
		User user = CommonUtils.toBean(request.getParameterMap(), User.class);
		
		
		user.setUid(CommonUtils.uuid());
		user.setIntegral(10);
		user.setState(true);
		user.setType("1");
		user.setEmail("null");
		user.setIp("null");
		
		try {
			userService.add(user);
			
			return findAll(request, response);
		} catch (UserException e) {
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("user", user);
			return "f:/adminjsps/admin/user/add.jsp";
		}
	}
	/**
	 * 管理员对用户的查询
	 */
	public String findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 1,调用service方法，得到所有学生信息
		 * 2，保存到request域，转发到adminjaps/admin/user/list.jsp";
		 */
		request.setAttribute("userList", userService.findAll());  
		return "f:/adminjsps/admin/user/list.jsp";
	}

	/**
	 * 管理员删除用户
	 */
	public String delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 1,获取参数：uid
		 * 2,调用service方法，传递uid参数
		 * 	>如果抛出异常，保存异常信息，转发到msg.jsp显示
		 * 3，调用findAll()
		 */
		String uid = request.getParameter("uid");
		try{
			userService.delete(uid);
			return findAll(request, response);
		}catch(UserException e){
			request.setAttribute("msg", e.getMessage());
			return "f:/adminjsps/msg.jsp";
		
		}
	}
	
	
	/*
	 * 管理员登录功能
	 */
	public String login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 1，封装表单数据到form中
		 * 2，输入校验
		 * 3，调用service完成激活
		 * 	>保存错误信息，form到request，转发到login.jsp
		 * 4，保存用户信息到session中，然后重定向到index.jsp
		 */
		User form = CommonUtils.toBean(request.getParameterMap(), User.class);
		/*
		 * 输入校验 1，创建一个Map,用来封装错误信息，其中key为表单字段名称，值为错误信息
		 */
		Map<String, String> errors = new HashMap<String, String>();
		String username = form.getUsername();
		if (username == null || username.trim().isEmpty()) {
			errors.put("username", "用户名不能为空!");
		}

		String password = form.getPassword();
		if (password == null || password.trim().isEmpty()) {
			errors.put("password", "密码不能为空!");
		} 
		/*
		 * 3，判断是否存在错误信息
		 */
		if (errors.size() > 0) {
			// 1，保存错误信息
			// 2，保存表单数据
			// 3，转发到regist.jsp
			request.setAttribute("errors", errors);
			request.setAttribute("form", form);
			return "f:/adminjsps/login.jsp";
		}
		/*
		 * 调用service的login方法
		 */
		try{
			User user = userService.adminLogin(form);
			request.getSession().setAttribute("session_adminUser", user);
			return "r:/adminjsps/admin/index.jsp";
		}catch(UserException e) {
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("form", form);
			return "f:/adminjsps/login.jsp";
		}
	}
}
